import { jobAxios } from '@/utils/request'
import { listByConcept, listRelationOrSchema } from '@/api/modular/main/knowl/relation/relationManage'
import { jobExecutorManagementList } from '@/api/modular/main/knowl/executor/executorManagement'

/**
 * 发布实体列表
 *
 * @author yang
 * @date 2021-06-03 22:50:57
 */
export function publishList(parameter) {
  const pageNo = parseInt(parameter['pageNo'])
  const pageSize = parseInt(parameter['pageSize'])
  parameter['start'] = (pageNo - 1) * pageSize
  parameter['length'] = pageSize
  return jobAxios({
    url: 'kgGraphPublish/pageList',
    method: 'get',
    params: parameter
  })
}

/**
 * 保存/修改发布实体
 *
 * @author yang
 * @date 2021-06-03 22:50:57
 */
export function addGraphPublish(parameter) {
  const cmd = parameter['id'] ? 'kgGraphPublish/edit' : 'kgGraphPublish/save'
  return jobAxios({
    url: cmd,
    method: 'post',
    data: parameter
  })
}

/**
 * 启动/停止发布
 *
 * @author yang
 * @date 2021-06-03 22:50:57
 */
export function publishStateTaskChange(parameter, stop) {
  const id = parameter['jobId']
  const cmd = stop ? 'jobinfo/stop?id=' + id : 'jobinfo/start?id=' + id
  return jobAxios({
    url: cmd,
    method: 'post',
    data: {
      'id': id
    }
  })
}

/**
 *
 * @author yang
 * @date 2021-06-03 22:50:57
 */
export function deletePublishEntity(parameter) {
  return jobAxios({
    url: 'kgGraphPublish/remove?id=' + parameter.id,
    method: 'post',
    data: {
      'id': parameter.id
    }
  })
}

/**
 *
 * @author yang
 * @date 2021-06-03 22:50:57
 */
export function queryPublishEntity(parameter) {
  return jobAxios({
    url: 'kgGraphPublish/detail',
    method: 'get',
    params: parameter
  })
}

/**
 * 关系挖掘_关系规则_详情 关系接口
 * @param {Object} schemaAParams
 * @param {Object} schemaBParams
 * @param {Object} conceptParams
 * @param {String} id
 */
export function publishEntityDetail(schemaAParams, schemaBParams, conceptParams, id) {
  return Promise.all([
    listRelationOrSchema(schemaAParams.schemaFrom, schemaAParams.id),
    listRelationOrSchema(schemaBParams.schemaFrom, schemaBParams.id),
    listByConcept(conceptParams),
    jobExecutorManagementList({}),
    queryPublishEntity({ 'id': id })])
    .then((result) => {
        return Promise.resolve([
          result[0].data,
          result[1].data,
          result[2].data,
          result[3].data,
          result[4].content
        ])
      }
    )
    .catch((e) => {
      return Promise.reject(e)
    })
}

/**
 * 执行一次
 *
 * @author yang
 * @date 2021-06-03 22:50:57
 */
export function publishJobTrigger(parameter) {
  return jobAxios({
    url: '/jobinfo/trigger',
    method: 'get',
    params: parameter
  })
}
